// Copyright (c) 2020 Presto Labs Pte. Ltd.
// Author: jhkim

#pragma once

#include <iostream>
#include <memory>
#include <optional>
#include <set>
#include <sstream>
#include <string>

#include <boost/algorithm/string.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

#include "coin2/exchange/apollox/symbology/currency.h"
#include "coin2/exchange/base/symbology/spot_product.h"
#include "coin2/exchange/base/symbology/util.h"

namespace coin2::exchange::apollox::symbology {

using ApolloxCurrency = coin2::exchange::apollox::symbology::ApolloxCurrency;

class ApolloxProduct : public base::symbology::SpotProduct<ApolloxCurrency, ApolloxProduct>,
                       public base::symbology::ProductFromStrMixin<ApolloxProduct> {
 public:
  typedef base::symbology::SpotProduct<ApolloxCurrency, ApolloxProduct> BaseProduct;

 public:
  ApolloxProduct(const ApolloxCurrency& base, const ApolloxCurrency& quote)
      : SpotProduct(
            base,
            quote,
            ToNativeProduct(base, quote),
            ToFeedNativeProduct(base, quote),
            base::market::ExchangeType::Apollox) {}

 public:
  static std::unique_ptr<ApolloxProduct> FromNativeStrImpl(
      const std::string& native_product,
      int64_t resolve_timestamp) {
    std::unique_ptr<ApolloxCurrency> base_ptr;
    std::unique_ptr<ApolloxCurrency> quote_ptr;
    bool success = ParseNativeProduct(native_product, resolve_timestamp, &base_ptr, &quote_ptr);
    if (success) {
      return std::make_unique<ApolloxProduct>(*base_ptr, *quote_ptr);
    } else {
      return {};
    }
  }

  const std::string& nav_native_symbol() const { return base().currency(); }

 private:
  static bool ParseNativeProduct(
      const std::string& product_str,
      int64_t resolve_timestamp,
      std::unique_ptr<ApolloxCurrency>* base_ptr,
      std::unique_ptr<ApolloxCurrency>* quote_ptr) {
    using base::symbology::HasValue;
    static const std::string quotes[] = {
        "USDT",
    };

    const std::string& product_str_upper = StringToUpperCopy(product_str);

    for (const auto& quote_cand : quotes) {
      if (StringEndsWith(product_str_upper, quote_cand)) {
        size_t base_len = product_str_upper.size() - quote_cand.size();
        std::string base = product_str_upper.substr(0, base_len);
        *base_ptr = ApolloxCurrency::FromNativeStrNoThrow(base, resolve_timestamp);
        *quote_ptr = ApolloxCurrency::FromNativeStrNoThrow(quote_cand, resolve_timestamp);
        return true;
      }
    }
    return false;
  }

  static std::string ToNativeProduct(const ApolloxCurrency& base, const ApolloxCurrency& quote) {
    using base::market::MarketType;
    std::stringstream ss;
    ss << base.native_currency(MarketType::Spot) << quote.native_currency(MarketType::Spot);
    return ss.str();
  }

  static std::string ToFeedNativeProduct(
      const ApolloxCurrency& base,
      const ApolloxCurrency& quote) {
    return StringToLowerCopy(ToNativeProduct(base, quote));
  }
};

}  // namespace coin2::exchange::apollox::symbology
